<?php
class employeeInfo extends Controller {
	var $msg = '';
	
	function employeeInfo()
	{
		parent::Controller();
	}
	
	function index()
	{
		if ($this->session->userdata('logged_in') != TRUE)
	    {
	        redirect('login/index');
	    }
		$data['form_status'] = 'add';
		$this->load->model('SchoolModel');
		$data['schoolList'] = $this->SchoolModel->getAll();
		
		$this->load->view('header');
		$this->load->view('employee-list-form', $data);
		$this->load->view('footer');
	}
	function search()
	{
		if ($this->session->userdata('logged_in') != TRUE)
	    {
	        redirect('login/index');
	    }
	    $criteria = array();
	    
	    $criteria['name'] = $this->input->post('txtName') ? $this->input->post('txtName') : null;
	    $criteria['emp_status'] = $this->input->post('empStatus');
	    $criteria['job_position'] = $this->input->post('txtJobPosition') ? $this->input->post('txtJobPosition') : null;
	    $criteria['tel'] = $this->input->post('txtTel') ? $this->input->post('txtTel') : null;
	    $criteria['email'] = $this->input->post('txtEmail') ? $this->input->post('txtEmail') : null;
	    
		$sc = $this->input->post('chkSchool');
		$schoolwhere = "";
		if($sc) {
			$schoolwhere = " and tusc.school_id in ( ";
			for($ii = 0; $ii < count($sc); $ii++) {
				$schoolwhere .= $sc[$ii];
				if($ii + 1 < count($sc)) {
					$schoolwhere .= ",";
				}
			}
			$schoolwhere .= ") ";
		}
		$criteria['school'] = $schoolwhere;
		
	    $this->load->model('empModel');
	    $result['result'] = $this->empModel->empSearch($criteria);
	    
	    $this->load->model('SchoolModel');
		$result['schoolList'] = $this->SchoolModel->getAll();
		
	    $this->load->view('header');
		$this->load->view('employee-list-form', $result);
		$this->load->view('footer');
	}
	
	
	function delete(){
		if ($this->session->userdata('logged_in') != TRUE)
	    {
	        redirect('login/index');
	    }
	    
	    $this->load->model('empModel');
	    $id = $this->input->post('txtDel');
	    for($i = 0; $i < count($id); $i++) {
	    	$data['employee_id'] = $id[$i];
	    	$this->empModel->delete($data);
	    }	    
	    $this->search();
	}
	
	function changePassword(){
		if ($this->session->userdata('logged_in') != TRUE)
	    {
	        redirect('login/index');
	    }
	    $this->load->view('header');
		$this->load->view('employee-changepwd-form');
		$this->load->view('footer');
	   
	}
	
	function savePassword(){
		if ($this->session->userdata('logged_in') != TRUE)
	    {
	        redirect('login/index');
	    }
	    $this->load->model('usermodel');
	    $this->load->helper(array('form', 'url', 'security'));
	    $passwordOld = $this->input->post('txtOldPass');
	    $passwordNew1 = $this->input->post('txtNewPass1');
	    $passwordNew2 = $this->input->post('txtNewPass2');
	    
	    $data = $this->usermodel->getUserByUsername($this->session->userdata('username'));
		if(dohash($passwordOld) == $data['password'] && $passwordNew1 == $passwordNew2) {
			$userdata['username'] = $this->session->userdata('username');
			$userdata['password'] = $passwordNew1;
			$this->usermodel->updateUser($userdata);
			$msg['msg'] = "บันทึกข้อมูลเรียบร้อย";
		} else {
			$msg['msg'] = "ไม่สามารถบันทึกข้อมูลได้: รหัสผ่านเดิมไม่ถูกต้อง";
		}
	    
	    
	    $this->load->view('header');
		$this->load->view('employee-changepwd-form');
		$this->load->view('footer', $msg);
	   
	}
}